/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.InqueryDAOInterface;
import com.wii.elpro.dao.hb.InqueryDAOImpl;
import com.wii.elpro.entity.Inquery;
import com.wii.elpro.entity.InqueryItem;
import com.wii.elpro.entity.Spdcf;
import com.wii.elpro.entity.SpdcfItem;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.SpdcfForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author User
 */
public class SpdcfMarketingSaveAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }          
        InqueryDAOInterface inq = new InqueryDAOImpl();
        Spdcf spdcf = new Spdcf();
        SpdcfForm f = (SpdcfForm)form;
        try{
            Inquery in = inq.getInquery(f.getId());
            request.setAttribute("idInquery", in.getId());
            request.setAttribute("idBusinessPartner", in.getProject().getNamaOwner().getId());
            List list = in.getItems();
            List list1 = new ArrayList();
            for(int x=0;x<list.size();x++){
                InqueryItem it = (InqueryItem)list.get(x);
                SpdcfItem si = new SpdcfItem();
                si.setProduk(it.getProduk());
                si.setJumlah(it.getJumlah());
                list1.add(si);                
            }
            sess.setAttribute("listSpdcfItem", list1);
            sess.setAttribute("inquery", in);
        }catch(Exception e){
            e.printStackTrace();
        }
        return mapping.findForward(SUCCESS);
        
    }
}